<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\web\Response;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Campaign;

use app\services\CampaignService;

class LaunchController extends AuthController
{
    public $currentHeader = 'header-campaign';
    public $currentNav = 'launch-nav';
    public $pageTitle = '';

    public function init() {
        parent::init();
        $this->service = new CampaignService();
        // $this->labels = $this->getAll('CampaignLabel', ['advertiser_id'=>$this->S('advertiser_id')]);
        $this->bindLabelId = $this->I('label_id');
        $sql = "select * from {{%condition_frequency_type}} where id !=1";
        $this->frequencyTypes = $this->query($sql);
    }

    public function actionLaunchList() {   
        $this->pageTitle = $this->t('campaign', 'Campaign List');
        $this->label_id = $this->I('label_id');
        $this->brandShow = true;
        $this->filter = $this->isAllowedSeeShows();
        return $this->show('launchList');
    }

    public function actionAddNewLaunch() {
        if($this->I('label_id')){
            $label = $this->getOne('CampaignLabel',['id'=>$this->I('label_id')]);
            $this->S('advertiser_id',$label['advertiser_id']);
        }
        $this->pageTitle = $this->t('campaign', 'New Campaign');
        $this->S('solution_id', null);
        $this->S('id', null);
        $this->totalBudget = $this->service->getTotalbudget();
        $this->labels = $this->service->getCampaignLabels();
        return $this->show('newLaunch');
    }

    public function actionLaunchEdit () {
        $this->pageTitle = $this->t('campaign', 'Edit Campaign');
        $this->S('solution_id', null);
        $this->totalBudget = $this->service->getTotalbudget();
        $campaign = $this->M('Campaign', $this->I('id'));
        $this->campaign = $campaign;
        $this->labels = $this->service->getCampaignLabels($this->I('id'));
        return $this->show();
    }

    public function actionNewLaunchHandle() {
        $_POST['name'] = $this->dropBlank($_POST['name']);
        $uniqueKey = ['name' => $this->I('name'), 'advertiser_id' => $this->S('advertiser_id')];
        $campaign = $this->getOne('Campaign', $uniqueKey);
        if($_POST['frequency_control']==0){
            $_POST['frequency_control_type'] = 0;
            $_POST['frequency_upper_limit'] = 0;
            $_POST['frequency_lower_limit'] = 0;
            $_POST['frequency_lower_start'] = '0000-00-00';
        }else{
            $dateRange = $_POST['range_date'];
            $rangeArr = explode(' - ', $dateRange);
            $_POST['frequency_lower_start'] = $rangeArr[0];
        }
        if ($campaign) {
            $this->NG($this->t('campaign', 'Campaign name already exists!'), '', $campaign);
        }
        $campaign = $this->save('Campaign', $uniqueKey);
        if ($campaign) {
            $this->OK($this->t('campaign', 'Create campaign successfully!'), '', $campaign);
        }
        $this->NG($this->t('campaign', 'Create campaign failed!'));
    }

    public function actionLaunchEditHandle() {
        $_POST['name'] = $this->dropBlank($_POST['name']);
        if($_POST['frequency_control']==0){
            $_POST['frequency_control_type'] = 0;
            $_POST['frequency_upper_limit'] = 0;
            $_POST['frequency_lower_limit'] = 0;
            $_POST['frequency_lower_start'] = '0000-00-00';
        }else{
            if(empty($_POST['frequency_lower_start'])){
                $dateRange = $_POST['range_date'];
                $rangeArr = explode(' - ', $dateRange);
                $_POST['frequency_lower_start'] = $rangeArr[0];
            }
            if(empty($_POST['frequency_lower_limit'])){
                $_POST['frequency_lower_limit'] = 0;
            }

        }
        $campaign = $this->save('Campaign');
        if (is_array($campaign) && is_numeric($campaign['id'])) {
            $this->OK($this->t('campaign', 'Edit campaign successfully!'), '', ['id'=>$campaign['id']]);
        }
        $this->NG($this->t('campaign', 'Edit campaign failed!'));
    }

    public function actionGetCampaigns() {
        $service = new \app\services\CampaignService();
        $campaigns = $service->getAllCampaigns();
        $this->jsonReturn(['data' => $campaigns]);
    }
}//end of LaunchController declaration
